package com.study_security.controller;

import com.study_security.domain.LoginUser;
import com.study_security.entity.Project;
import com.study_security.mapper.ProjectMapper;
import com.study_security.service.AuditService;
import com.study_security.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.study_security.utils.RedisConstants.PROJECT_KEY;

@RestController
@RequestMapping("/api/audit")
@PreAuthorize("hasAuthority('perm_review')")
public class AuditController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private AuditService auditService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取所有项目
     *
     * @return 所有项目的列表
     */
    @GetMapping
    public List<Project> getAllProjects() {
        return projectService.getAllProject();
    }

    /**
     * 获取待审核的项目
     *
     * @return 待审核项目的列表
     */
    @GetMapping("/pending")
    public List<Project> searchPendingProjects() {
        return auditService.findPending();
    }

    /**
     * 获取已结项的项目
     *
     * @return 已结项项目的列表
     */
    @GetMapping("/approved")
    public List<Project> searchApproved() {
        return auditService.findApproved();
    }

    /**
     * 获取在途项目
     *
     * @return 在途项目列表
     */
    @GetMapping("/ongoing")
    public List<Project> getOngoingProjects() {
        return auditService.findOngoing();
    }

    /**
     * 根据项目名称搜索在途项目
     *
     * @param projectName 项目名称
     * @return 在途的项目列表
     */
    @GetMapping("/ongoing/{projectName}")
    public List<Project> searchOngoingProjectsByName(@PathVariable("projectName") String projectName) {
        return auditService.findOngoingByName(projectName);
    }

    /**
     * 根据项目名称搜索已结项的项目
     *
     * @param projectName 项目名称
     * @return 已结项的项目列表
     */
    @GetMapping("/pending/{projectName}")
    public List<Project> searchPendingProjectsByName(@PathVariable("projectName") String projectName) {
        return auditService.findPendingByName(projectName);
    }

    /**
     * 根据项目名称搜索已审核的项目
     *
     * @param projectName 项目名称
     * @return 已审核的项目列表
     */
    @GetMapping("/approved/{projectName}")
    public List<Project> searchApprovedProjectsByName(@PathVariable("projectName") String projectName) {
        return auditService.findApprovedByName(projectName);
    }

    /**
     * 更新项目状态
     *
     * @param projectId 项目ID
     * @param isApproved 是否批准
     * @return 更新结果
     */
    @PostMapping("/updateStatus")
    public ResponseEntity<String> updateProjectStatus(@RequestParam Long projectId, @RequestParam int isApproved) {
        boolean approved = (isApproved == 1);
        boolean success = auditService.updateProjectStatus(projectId, approved);
        Project p = projectService.getProjectById(projectId);
        System.out.println(p.getProjectStatus());
        if (success) {
            Long responsibleId = p.getResponsibleId();
            stringRedisTemplate.delete(PROJECT_KEY+responsibleId);
            return ResponseEntity.ok("Project status updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update project status");
        }
    }

    /**
     * 获取项目概述
     *
     * @param projectId 项目ID
     * @return 项目详情
     */
    @GetMapping("/project/{projectId}")
    public Project getProjectOverview(@PathVariable Long projectId) {
        return projectService.getProjectById(projectId);
    }
}
